#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <map>
#include <algorithm>

namespace cpp23 {

// 在C++23之前的解构
void traditional_destructuring() {
  std::cout << "传统解构方式:" << std::endl;
  
  // 使用tuple
  std::tuple<int, std::string, double> data(42, "测试", 3.14);
  auto [id, name, value] = data;
  std::cout << "  解构tuple: id=" << id << ", name=" << name << ", value=" << value << std::endl;
  
  // 使用pair
  std::pair<std::string, int> entry("键", 100);
  auto [key, val] = entry;
  std::cout << "  解构pair: key=" << key << ", val=" << val << std::endl;
  
  // 使用结构体
  struct Point { int x; int y; };
  Point p{10, 20};
  auto [x, y] = p;
  std::cout << "  解构结构体: x=" << x << ", y=" << y << std::endl;
}

// 使用C++23自动解构特性的lambda
void auto_destructuring_in_lambda() {
  std::cout << "C++23 lambda中的自动解构:" << std::endl;
  
  std::vector<std::pair<std::string, int>> items = {
    {"苹果", 5},
    {"香蕉", 3},
    {"橙子", 7}
  };
  
  // C++20写法
  std::cout << "  C++20 lambda写法:" << std::endl;
  std::for_each(items.begin(), items.end(), 
    [](const auto& item) {
      const auto& [name, count] = item;
      std::cout << "    " << name << ": " << count << std::endl;
    }
  );
  
  // C++23写法 - 注意：GCC 13可能不完全支持lambda参数中的结构化绑定
  std::cout << "  C++23 lambda写法（注意：GCC 13可能不支持）:" << std::endl;
  // 注释掉GCC 13不支持的语法
  /*
  std::for_each(items.begin(), items.end(), 
    [](const auto& [name, count]) {
      std::cout << "    " << name << ": " << count << std::endl;
    }
  );
  */
  // 使用C++20兼容语法
  std::for_each(items.begin(), items.end(), 
    [](const auto& item) {
      const auto& [name, count] = item;
      std::cout << "    " << name << ": " << count << std::endl;
    }
  );
}

// 使用C++23自动解构处理对
void process_pairs() {
  std::cout << "处理键值对:" << std::endl;
  
  std::map<std::string, int> scores = {
    {"张三", 95},
    {"李四", 87},
    {"王五", 92}
  };
  
  // 非C++23写法
  std::cout << "  传统写法:" << std::endl;
  for (const auto& entry : scores) {
    std::cout << "    " << entry.first << " 得分: " << entry.second << std::endl;
  }
  
  // C++23写法
  std::cout << "  C++23写法:" << std::endl;
  for (const auto& [name, score] : scores) {
    std::cout << "    " << name << " 得分: " << score << std::endl;
  }
}

// 在函数参数中使用自动解构
// 注意：GCC 13不支持函数参数中的结构化绑定，使用传统方式
template<typename Tuple>
void print_person(const Tuple& person_tuple) {
  const auto& [name, age, city] = person_tuple;
  std::cout << "  姓名: " << name << ", 年龄: " << age << ", 城市: " << city << std::endl;
}

// 利用自动解构返回多个值
std::tuple<std::string, int, double> get_product_info(int product_id) {
  // 模拟数据库查询
  if (product_id == 1) {
    return {"手机", 1299, 4.5};
  } else if (product_id == 2) {
    return {"笔记本电脑", 5999, 4.8};
  } else {
    return {"未知产品", 0, 0.0};
  }
}

// 处理复杂类型
struct Employee {
  std::string name;
  int id;
  struct {
    std::string street;
    std::string city;
  } address;
};

void complex_destructuring() {
  std::cout << "复杂类型解构:" << std::endl;
  
  Employee emp{"张明", 1001, {"人民路", "北京"}};
  
  // 嵌套解构
  auto& [name, id, address] = emp;
  auto& [street, city] = address;
  
  std::cout << "  员工: " << name << " (ID: " << id << ")" << std::endl;
  std::cout << "  地址: " << street << ", " << city << std::endl;
}

// 自动解构主演示函数
void demonstrate_auto_destructuring() {
  std::cout << "演示C++23自动解构特性\n" << std::endl;
  
  traditional_destructuring();
  
  std::cout << std::endl;
  auto_destructuring_in_lambda();
  
  std::cout << std::endl;
  process_pairs();
  
  std::cout << std::endl;
  std::cout << "函数参数中的自动解构:" << std::endl;
  std::tuple<std::string, int, std::string> person{"李华", 28, "上海"};
  print_person(person);
  
  std::cout << std::endl;
  std::cout << "使用自动解构接收函数返回值:" << std::endl;
  auto [product_name, price, rating] = get_product_info(2);
  std::cout << "  产品: " << product_name << ", 价格: " << price << "元, 评分: " << rating << std::endl;
  
  std::cout << std::endl;
  complex_destructuring();
  
  std::cout << "\n自动解构的优势:" << std::endl;
  std::cout << "1. 简化lambda表达式" << std::endl;
  std::cout << "2. 提高代码可读性" << std::endl;
  std::cout << "3. 减少中间变量" << std::endl;
  std::cout << "4. 使函数参数更清晰" << std::endl;
  std::cout << "5. 更优雅地处理多值返回" << std::endl;
}

} // namespace cpp23